package SparkExercises.kejian8;

import org.apache.spark.sql.AnalysisException;
import org.apache.spark.sql.Dataset;
import org.apache.spark.sql.Row;
import org.apache.spark.sql.SparkSession;
import org.apache.spark.sql.api.java.UDF1;
import org.apache.spark.sql.types.DataTypes;

/**
 * 请写一个Spark SQL程序，
 * 将student.json的手机号中间部分用星号代替，
 * 前三位和后四位保留，并将结果格式化输出到控制台
 */
public class LianXi {
    public static void main(String[] args) throws AnalysisException {
        SparkSession sparkSession = SparkSession.builder().appName(LianXi.class.getName()).master("local").getOrCreate();

        Dataset<Row> json = sparkSession.read().json("./data/student.json");

        json.createTempView("student");

        sparkSession.udf().register("adult", new UDF1<Long, String>() {
            @Override
            public String call(Long l) throws Exception {
                String s = Long.toString(l);
                String s1 = s.substring(0, 3) + "*" + s.substring(7, 11);

                return s1;
            }
        }, DataTypes.StringType);

        sparkSession.sql("select age,institute,name, adult(phone) as `phone`,sex from student").show();

    }
}
